/**
 * author      : dormi330
 * date        : 2018/6/26
 * project     : mybatis
 * description : 应用层服务
 */

package com.deep.trade.application.order;

import com.deep.trade.adaptor.required.SellerMgmtService;
import com.deep.trade.common.TradeException;
import com.deep.trade.domain.order.OrderQueryRepository;
import com.deep.trade.domain.order.OrderRepository;
import com.deep.trade.domain.order.TradeOrder;
import com.deep.trade.infrastructure.utils.CommonUtils;
import com.deep.trade.order.InDtoAuditOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AuditOrderService {

    @Autowired
    private OrderRepository orderRepository;
    private static final int ONLY_ONE_SHOULD_BE_UPDATED = 1;

    @Autowired
    private OrderQueryRepository orderQueryRepository;

    @Autowired
    private SellerMgmtService sellerMgmtService;

    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    /**
     * 获取 TradeOrder 订单, 并确保存在
     *
     * @param tenantCode   租户code
     * @param tradeOrderId id
     * @return TradeOrder
     * @throws Exception
     */
    private TradeOrder assureTradeOrder(String tenantCode, String tradeOrderId) throws Exception {
        TradeOrder tradeOrder = orderQueryRepository.tradeOrderOfId(tenantCode, tradeOrderId);
        if (null == tradeOrder) {
            String errMsg = String.format("no such tradeOrder, tenantCode=%s,  tradeOrderId=%s", tenantCode, tradeOrderId);
            throw new TradeException(errMsg);
        }
        return tradeOrder;
    }

    /**
     * 审核一个订单, 无须事务控制
     *
     * @param inDtoAuditOrder
     * @throws Exception
     */
    public void auditTradeOrder(InDtoAuditOrder inDtoAuditOrder) throws Exception {
        logger.info("{}, {}", CommonUtils.currentClassAndMethod(), inDtoAuditOrder);

        // ====== public 入参检测 =======
        if (null == inDtoAuditOrder) throw new TradeException("arguments cannot be null");

        String tenantCode = inDtoAuditOrder.getTenantCode();
        if (null == tenantCode) throw new TradeException("tenantCode cannot be null");

        String tradeOrderId = inDtoAuditOrder.getTradeOrderId();
        if (null == tradeOrderId) throw new TradeException("tradeOrderId cannot be null");

        // ====== 获取领域对象 =======
        TradeOrder tradeOrder = assureTradeOrder(tenantCode, tradeOrderId);

        // ====== 权限检测 =======
        boolean canAudit = sellerMgmtService.canUserAuditOrder(inDtoAuditOrder.getAuditorId(), tradeOrder.getSellerId());
        if (!canAudit) throw new Error("cannot auditOne this order");

        // ====== 领域对象操作 =======
        tradeOrder.aduit(inDtoAuditOrder.isApprove(), inDtoAuditOrder.getRejectReason());

        // ====== 存储 =======
        if (ONLY_ONE_SHOULD_BE_UPDATED != orderRepository.updateTradeOrderStatus(tradeOrder)) {
            throw new TradeException("audit fail, cannot get order to audit");
        }
    }
}
